package 二叉树.printer;

/**

             ┌──800
         ┌──760
         │   └──600
     ┌──540
     │   └──476
     │       └──445
 ┌──410
 │   └──394
381
 │     ┌──190
 │     │   └──146
 │  ┌──40
 │  │  └──35
 └──12
    └──9
 
 * @author MJ Lee
 *
 */
public class InorderPrinterTest extends Printer {
	private static String rightAppend;
	private static String leftAppend;
	private static String blankAppend;
	private static String lineAppend;
	static {
		int length = 2;
		rightAppend = "┌" + Strings.repeat("─", length);
		leftAppend = "└" + Strings.repeat("─", length);
		blankAppend = Strings.blank(length + 1);
		lineAppend = "│" + Strings.blank(length);
	}

	public InorderPrinterTest(BinaryTreeInfo tree) {
		super(tree);
	}

	@Override
	public String printString() {
		StringBuilder string = new StringBuilder(
				printString(tree.root(), "", "", ""));
		string.deleteCharAt(string.length() - 1);
		return string.toString();
	}
	
	/**
	 * 生成node节点的字符串
	 * @param nodePrefix node那一行的前缀字符串
	 * @param leftPrefix node整棵左子树的前缀字符串
	 * @param rightPrefix node整棵右子树的前缀字符串
	 * @return
	 */
	private String printString(Object node, String nodePrefix, String leftPrefix, String rightPrefix) {
		Object left = tree.left(node);
		Object right = tree.right(node);
		String string = tree.string(node).toString(); // 节点字符串
		
		int length = string.length();
		if (length % 2 == 0) {
			length--;
		}
		length >>= 1;

		String nodeString = ""; // 重置待输出字符串
		if (right != null) {
			rightPrefix += Strings.blank(length);
			String rightString = printString(right, rightPrefix + rightAppend, rightPrefix + lineAppend, rightPrefix + blankAppend);
			nodeString += rightString;

			System.out.println("右节点值:");
			System.out.println(rightString);
//			System.out.println("待输出字符串:");
//			System.out.println(nodeString);
			System.out.println("---------------");
		}
		nodeString += nodePrefix + string + "\n";
		System.out.println("node那行的前缀：");
		System.out.println(nodePrefix);

		if (left != null) {
			leftPrefix += Strings.blank(length);
			String leftString = printString(left, leftPrefix + leftAppend, leftPrefix + blankAppend, leftPrefix + lineAppend);
			nodeString += leftString;

			System.out.println("左节点值:");
			System.out.println(leftString);
//			System.out.println("待输出字符串:");
//			System.out.println(nodeString);
			System.out.println("---------------");
		}

		System.out.println("输出nodeString:");
		System.out.println(nodeString);
		return nodeString;
	}
}
